import { h } from '@vue/runtime-core'
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]


const router = createRouter({
  history: createWebHistory(process.env.BASE_URL), // 新增：base选项作为参数传递
  routes,
  scrollBehavior(to, from, savePos) { // 路由发生变化后返回上一层记录上一次的访问位置
    if(savePos) {
      return savePos
    }else{
      return {top: 0}
    }
  }
})

// 路由新增：addRoute()
router.addRoute({
  name: 'demo',
  path: '/demo',
  component: () => import('../components/demo.vue')
})
// 路由添加具体路由下的子节点
router.addRoute('demo',{
  name: 'demoChildren',
  path: '/demo/demoChildren',
  component: {
    render() {
      return h('div', 'demoChildren pages...')
    }
  }
})

export default router
